<template>
  <div class="dashboard-container">
    <div class="app-container">
      <el-card class="tree-card">
        <!-- 用了一个行列布局 -->
        <!-- 顶部数据 -->
        <tree-tools
          :node-data="companyInfo"
          :is-company="true"
          @showAddDialog="showAddDialog"
        />
        <hr>
        <!-- 树形列表 -->
        <!-- 树形结构自动展开 ：default-expand-all -->
        <el-tree :data="depts" default-expand-all>
          <template v-slot="scope">
            <tree-tools
              :node-data="scope.data"
              :is-company="false"
              @showAddDialog="showAddDialog"
              @showEditDialog="showEditDialog"
              @reload="loadPage"
            />
          </template>
        </el-tree></el-card>
      <add-depts
        ref="addDepts"
        :is-show-dialog="isShowDialog"
        :node-data="nodeData"
        @closeDialog="isShowDialog=false"
        @reload="loadPage"
      />
    </div>
  </div>
</template>

<script>
import TreeTools from './components/TreeTools'
import { getDepartments } from '@/api/departments'
import { listToTree } from '@/utils/index'
import AddDepts from './components/AddDepts.vue'
export default {
  components: {
    TreeTools,
    AddDepts
  },
  data() {
    return {
      companyInfo: {
        name: '中国积极向上科技有限公司',
        manager: '负责人',
        id: ''
      },
      depts: [],
      // 新增对话框隐藏
      isShowDialog: false,
      // 临时储存被点击的对象  TreeTools
      nodeData: {}
    }
  },
  created() {
    this.loadPage()
  },
  methods: {
    // 获取树形数据列表
    async  loadPage() {
      const { depts } = await getDepartments()
      this.depts = listToTree(depts, '')
    },
    showAddDialog(nodeData) {
      this.isShowDialog = true
      // 树形行组件, 被点击时, 被点击的对象往外传,
      // 父组件暂存传给弹窗
      this.nodeData = nodeData
    },
    showEditDialog(nodeData) {
      this.$refs.addDepts.getDepartDetail(nodeData.id)
      // 显示弹窗
      this.isShowDialog = true
    }
  }
}
</script>

  <style>

  </style>
